//
//  MainTabBarController.m
//  TabBar框架
//
//  Created by MyMac on 15/9/24.
//  Copyright (c) 2015年 tongfang. All rights reserved.
//

#import "MainTabBarController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "BaseNavigationViewController.h"
@interface MainTabBarController ()
@property(nonatomic,strong)UIViewController*currentVC;
@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    AViewController*Avc=[[AViewController alloc]init];
    self.currentVC=Avc;
  
    
    
    BViewController*Bvc=[[BViewController alloc]init];

    
    CViewController*Cvc=[[CViewController alloc]init];
 
    self.viewControllers=@[Avc,Bvc,Cvc];
    NSArray*barItem=@[@"A",@"B",@"C"];
    self.tabBar.hidden=YES;
    UIView*tabBarView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, self.view.bounds.size.width, 49)];
    tabBarView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:tabBarView];
    for (NSUInteger i=0; i<barItem.count; i++) {
        CGFloat w=self.view.bounds.size.width/barItem.count;
        CGFloat h=self.tabBar.bounds.size.height;
        CGFloat y=0;
        CGFloat x=i*w;
        UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
        btn.tag=i;
        [btn setTitle:barItem[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside];
        
    
        [tabBarView addSubview:btn];
    }
    //要在toVC走了viewDidLoad后才能item转换，那时toVC的item才有值
    
    [self itemFrom:nil itemTo:Avc];
    
}
-(void)select:(UIButton*)btn
{
    
    
    UIViewController*fromVC=self.currentVC;
    UIViewController*toVC=self.viewControllers[btn.tag];
    

    self.selectedIndex=btn.tag;
    
    self.currentVC=toVC;
[self itemFrom:fromVC itemTo:toVC];

}
//全局只有一个导航控制器，要想实现控制器导航item的切换，实际上操作的是tabBarVC的导航控制器，TabBarVC里面管制的控制器并不在导航控制器管制里面，无法实现导航控制器item的自动切换
-(void)itemFrom:(UIViewController*)fromVC itemTo:(UIViewController*)toVC
{
  self.navigationItem.rightBarButtonItem=toVC.navigationItem.rightBarButtonItem;
    self.navigationItem.title=toVC.navigationItem.title;
    self.navigationItem.rightBarButtonItems=toVC.navigationItem.rightBarButtonItems;
    self.navigationItem.leftBarButtonItem=toVC.navigationItem.leftBarButtonItem;
    self.navigationItem.leftBarButtonItems=toVC.navigationItem.leftBarButtonItems;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
